/********************************
 * ETK - Easy ToolKit
 * Copyright (C) 2007-2008 Ekinoks (scv.ekinoks@gmail.com)
 * All rights reserved.
 *
 *    This file is part of ETK.
 *
 *    Foobar is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    Foobar is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with Foobar; if not, write to the Free Software
 *    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 ***********************************************************************************/

#include "Widget.h"

#include <iostream>
#include <exception>
#include <set>

#include "../Render/Render.h"
#include "../Render/ToolKit/ToolKit.h"

#include "../CallBack/CallBack.h"
#include "../CallBack/CallBackI/CallBackT/CallBackT.h"

#include "../Widget/WidgetContainer/WidgetContainer.h"

#include <gtk/gtk.h>


namespace etk
{

    Widget::MyContainer::MyContainer(Widget *me, WidgetContainer * value) : me_(me), value_(value)
    {
        // s'ajouter au container
        if(value_ != NULL)
            value_->add(me_);
    }

    Widget::MyContainer::~MyContainer()
    {
        start_section("~MyContainer");
        // On dit a sont conteneur qu'on existe plus
        if(value_!=NULL)
            value_->remove(me_);
        end_section("~MyContainer");
    }

/// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    Widget::Widget(RWidget *rWidget_, WidgetContainer * container): rWidget(rWidget_), myContainer(this, container)
    {
        // Protection : si on essaye de destroy directement en utilisant la ToolKit.
        new CallBackT<Event>(this, EVENT_DESTROY, boost::bind(&Widget::destroy, _ME), true);
    }

/*    RWidget* Widget::operator() () const
    {
        return rWidget;
    }*/

    CallBack* Widget::add_call_back(CallBack *callback)
    {
        listCallBack.insert(callback);
        return callback;
    }

    void Widget::remove_call_back(CallBack *callback)
    {
        listCallBack.erase(callback);
    }

    void Widget::show() const
    {
        TOOL_KIT.widget_show(getRWidget());
    }

    void Widget::hide() const
    {
        TOOL_KIT.widget_hide(getRWidget());
    }

    void Widget::destroy()
    {
        delete this;
    }

    Widget::~Widget()
    {
        start_section("~Widget");
        // On suprime tout les CallBack
        for (std::set<CallBack*>::iterator it=listCallBack.begin() ; it != listCallBack.end() ; it++ )
            delete *it;

        // On suprime l'objet graphique
        delete rWidget;
        end_section("~Widget");
    }

    RWidget* Widget::getRWidget() const
    {
        return rWidget;
    }




}

